#include "utility.hh"

void
Utility::to_oct (int n, char* res)
{
  for (int i = 0; i < 4; ++i)
    res[i] = 0;

  for (int i = 0; i < 32; ++i)
  {
    if (n % 2)
    {
      res[i / 8] += (1 << (i % 8));
    }
    n >>= 1;
  }
}

int
Utility::from_oct (unsigned char* oct)
{
  int res = 0;
  int j = 0;
  for (int i = 0; i < 4; ++i)
  {
    unsigned char tmp = oct[i];
    for (int k = 0; k < 8; ++k)
    {
      if (tmp % 2)
	res |= (1 << j);
      ++j;
      tmp >>= 1;
    }
  }
  return res;
}

int
Utility::from_oct (const char* oct)
{
  int res = 0;
  int j = 0;
  for (int i = 0; i < 4; ++i)
  {
    unsigned char tmp = oct[i];
    for (int k = 0; k < 8; ++k)
    {
      if (tmp % 2)
	res |= (1 << j);
      ++j;
      tmp >>= 1;
    }
  }

  return res;
}
